package com.example.tests;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;

public class TestBase {

	protected ApplicationManager app;
		
	@BeforeTest
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(configFile)));
		app = ApplicationManager.getInstance();
		app.setProperties(properties);
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
	}
	
	@DataProvider
	public Iterator<Object[]> randomFolderGenerator(){
		return wrapFoldersForDataProvider(generateRandomFolders(5)).iterator();
	}
	
	public static List<Object[]> wrapFoldersForDataProvider(List<String> folders) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (String folder : folders) {
			list.add(new Object[]{folder});			
		}
		return list;
	}
	
	public static List<String> generateRandomFolders(int amount) {
		List<String> list = new ArrayList<String>();		
		for (int i = 0; i < amount; i++) {
			list.add(generateRandomString("folder"));
		}
		return list;
	}
	
	public static String generateRandomString(String text){
		Random rnd = new Random();
		return text + rnd.nextInt();
	}

}
